//-------
// 请求方法
//----
jQuery.extend({
	//AJAX封装方法： $.ajaxs(url, params, successFn, _async)
	//【后台数据返回格式】为：{code: 0, data: "", msg: ''}
	ajaxs:function(url,params,successFn,async,isShowLoad){
		return new Promise(function (resolve, reject) {
			var _loadingIdx=999999999999;
			$.ajax({
				type: "post",
				url:__webConfig__.web_entrance+url,
				data: params,
				timeout: 10000,
				dataType:"json",
				contentType: typeof params == "string" ? "application/json" :  "application/x-www-form-urlencoded",
				async:async === false ? false : true,
				beforeSend: function(xhr) {
					if(isShowLoad!==false){
						_loadingIdx = layer.load(1);
					}
					if(window.__token__){
						xhr.setRequestHeader("token",window.__token__);
					}
				},
				success: function(resp) {
				    var msg = resp.msg || resp.info;				    
					if(resp.code == 0) {
						if(successFn){successFn(resp);}
						resolve(resp);
					}else if(resp.code == -2){//错误 
	                    $.alert(2, msg);
	                    reject(resp);
	                }else if(resp.code == -10){//未登陆
	                    $.alert(0, msg,function(){
	                       window.location.href= window.location.href;
	                    });
	                }else{
	                    $.alert(0, (msg || "错误")+"<br/>"+( resp.data || ''));
	                    reject(resp);
	                }
				},
				complete: function() {
					layer.close(_loadingIdx);
				},
				error: function(xhr, textStatus, err) {
					var errText= xhr.responseText || '';
	                errText=errText.substring(errText.indexOf("<h1>",0)+4,errText.indexOf("</h1>",0));
				    var errMsg = errText + "<br/>状态码是： " + xhr.status;
				    if(errText){
				    	$.alert(2, errMsg);
				    }else{
				    	reject({'msg':errMsg});
				    }
				}
			});
        });
	},
	//公共接口-获取数据
	// $.getData(param,successFn,_async)
	// param={from:"xw_nav",field:"nav_id,nav_name",where:"nav_id=1"}
	getData : function(param,successFn,_async){
	    var param= param || {};
	    return this.ajaxs("/admin/api/base/getData",param,function(resp){
	        if(successFn){successFn(resp);}
	    },_async);
	},
	//公共接口-添加数据
    // $.addData(param,successFn,_async)
    // param={from:"nav",....}
	addData : function(param,successFn,_async){
        var param= param || {};
        return this.ajaxs("/admin/api/base/addData",param,function(resp){
            if(successFn){successFn(resp);}
        },_async);
    },
    //公共接口-删除数据
    // $.delData(param,successFn,_async)
    // param={from:"xw_nav",field:"nav_id,nav_name",where:"nav_id=1"}
    delData : function(param,successFn,_async){
        var param= param || {};
        return this.ajaxs("/admin/api/base/delData",param,function(resp){
            if(successFn){successFn(resp);}
        },_async);
    },
    //公共接口-更新数据
    // $.updateData(param,successFn,_async)
    // param={ from:"xw_nav",where:"nav_id",data:[  {nav_id:1,dd:222} ]  }
    updateData : function(param,successFn,_async){
        var param= param || {};
        return this.ajaxs("/admin/api/base/updateData",param,function(resp){
        	$.toast(1,resp.msg)
            if(successFn){successFn(resp);}
        },_async);
    }
});
